package com.galgram.project.controller;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;

import com.galgram.project.domain.*;
import com.galgram.project.entity.Collect;
import com.galgram.project.entity.Course;
import com.galgram.project.entity.News;
import com.galgram.project.entity.UserRecord;
import com.galgram.project.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.galgram.common.annotation.Log;
import com.galgram.common.core.controller.BaseController;
import com.galgram.common.core.domain.AjaxResult;
import com.galgram.common.enums.BusinessType;
import com.galgram.common.utils.poi.ExcelUtil;
import com.galgram.common.core.page.TableDataInfo;

/**
 * 用户收藏Controller
 *
 * @author galgram
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/project/collect")
public class FxCollectController extends BaseController {
    @Autowired
    private IFxCollectService fxCollectService;
    @Autowired
    private IFxNewsService fxNewsService;
    @Autowired
    private IFxPointsService fxPointsService;
    @Autowired
    private IFxCouponService fxCouponService;

    /**
     * 查询个人收藏列表
     */
    @GetMapping("/getList")
    public TableDataInfo getCollectList(Collect collect) {
        startPage();
        if (collect.getType().equals("热门课程")) {
            List<Course> courseList = new ArrayList<>();
            List<Collect> list = fxCollectService.getUserCollectList(collect);
            for (Collect collectCourse : list) {
                courseList.add(collectCourse.getCourse());
            }
            return getDataTable(courseList);
        }
        if (collect.getType().equals("教育头条")) {
            List<News> newsList = new ArrayList<>();
            List<Collect> list = fxCollectService.getUserCollectList(collect);
            for (Collect collectNews : list) {
                newsList.add(collectNews.getNews());
            }
            return getDataTable(newsList);
        }
        List<Collect> list = fxCollectService.getUserCollectList(collect);
        return getDataTable(list);
    }

    /**
     * 查询用户首页参数
     */
    @GetMapping("/getUserRecord/{userId}")
    public TableDataInfo getUserRecord(@PathVariable("userId") Long userId) {
        FxCollect fxCollect = new FxCollect();
        fxCollect.setUserId(userId);
        List<FxCollect> collectList = fxCollectService.selectFxCollectList(fxCollect);
        FxPoints fxPoints = new FxPoints();
        fxPoints.setUserId(userId);
        List<FxPoints> pointsList = fxPointsService.selectFxPointsList(fxPoints);
        FxCoupon fxCoupon = new FxCoupon();
        fxCoupon.setUserId(userId);
        List<FxCoupon> couponList = fxCouponService.getCouponList(fxCoupon);
        UserRecord record = new UserRecord();
        record.setUserId(userId);
        int pointNumber = 0;
        for (FxPoints points : pointsList) {
            pointNumber += points.getAmount();
        }
        record.setPointNumber(pointNumber);
        record.setCouponNumber(couponList.size());
        record.setCollectNumber(collectList.size());
        List<UserRecord> list = new ArrayList<>();
        list.add(record);
        return getDataTable(list);
    }

    /**
     * 查询用户收藏列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FxCollect fxCollect) {
        startPage();
        List<FxCollect> list = fxCollectService.selectFxCollectList(fxCollect);
        return getDataTable(list);
    }

    /**
     * 获取用户收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:collect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxCollectService.getById(id));
    }

    /**
     * 新增用户收藏
     */
    @Log(title = "用户收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxCollect fxCollect) {
        fxCollectService.save(fxCollect);
        if (fxCollect.getType().equals("教育头条")) {
            FxNews news = fxNewsService.getById(fxCollect.getNewsId());
            news.setCollect(news.getCollect() + 1);
            fxNewsService.updateById(news);
        }
        return success("添加成功");
    }

    /**
     * 删除用户指定收藏
     */
    @Log(title = "用户收藏", businessType = BusinessType.DELETE)
    @PutMapping("/removeCollect")
    public AjaxResult removeUserCollect(@RequestBody FxCollect fxCollect) {
        fxCollectService.deleteCollect(fxCollect);
        if (fxCollect.getType().equals("教育头条")) {
            FxNews news = fxNewsService.getById(fxCollect.getNewsId());
            news.setCollect(news.getCollect() - 1);
            fxNewsService.updateById(news);
        }
        return success("删除成功");
    }

    /**
     * 修改用户收藏
     */
    @PreAuthorize("@ss.hasPermi('project:collect:edit')")
    @Log(title = "用户收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxCollect fxCollect) {
        fxCollectService.updateById(fxCollect);
        return success("修改成功");
    }

    /**
     * 删除用户收藏
     */
    @PreAuthorize("@ss.hasPermi('project:collect:remove')")
    @Log(title = "用户收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        fxCollectService.removeByIds(ids);
        return success("删除成功");
    }

    /**
     * 导出用户收藏列表
     */
    @PreAuthorize("@ss.hasPermi('project:collect:export')")
    @Log(title = "用户收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxCollect fxCollect) {
        List<FxCollect> list = fxCollectService.selectFxCollectList(fxCollect);
        ExcelUtil<FxCollect> util = new ExcelUtil<FxCollect>(FxCollect.class);
        util.exportExcel(response, list, "用户收藏数据");
    }

    /**
     * 导入用户收藏列表
     */
    @PreAuthorize("@ss.hasPermi('project:collect:import')")
    @Log(title = "用户收藏", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<FxCollect> util = new ExcelUtil<FxCollect>(FxCollect.class);
        List<FxCollect> userList = util.importExcel(file.getInputStream());
        fxCollectService.saveBatch(userList);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模版
     */
    @PreAuthorize("@ss.hasPermi('project:collect:export')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<FxCollect> list = new ArrayList<FxCollect>();
        ExcelUtil<FxCollect> util = new ExcelUtil<FxCollect>(FxCollect.class);
        util.exportExcel(response, list, "用户收藏数据");
    }
}
